package com.etc.library.service.impl;

import com.etc.library.dao.BookMapper;
import com.etc.library.dao.MessageMapper;
import com.etc.library.dao.RecordMapper;
import com.etc.library.pojo.Book;
import com.etc.library.pojo.Message;
import com.etc.library.pojo.Record;
import com.etc.library.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Transactional
    @Override
    public boolean add(Record record) {

        //添加借阅记录
        recordMapper.add(record);
        //修改图书信息
        Book book = new Book();
        book.setBookId(record.getBookId());
        //根据图书编号获取到图书信息
        Map<String, Object> params = new HashMap<>();
        params.put("bookId", record.getBookId());
        Map<String, Object> map = bookMapper.list(params).get(0);
        int bookNum = Integer.parseInt(map.get("book_num").toString());
        int borrNum = Integer.parseInt(map.get("borr_num").toString());
        book.setBorrNum(borrNum + 1);
        bookNum--;
        if (bookNum <= 0) {
            bookNum = 0;
            book.setBState(1);
        }
        book.setBookNum(bookNum);
        bookMapper.update(book);

        return true;
    }

    @Override
    public List<Map<String, Object>> list(Map<String, Object> params) {
        return recordMapper.list(params);
    }

    @Transactional
    @Override
    public boolean revert(Record record) {
        //更新借阅记录
        recordMapper.update(record);
        //更新图书

        Book book = new Book();
        book.setBookId(record.getBookId());
        //根据图书编号获取到图书信息
        Map<String, Object> params = new HashMap<>();
        params.put("bookId", record.getBookId());
        Map<String, Object> map = bookMapper.list(params).get(0);
        int bookNum = Integer.parseInt(map.get("book_num").toString());

        book.setBookNum(bookNum+1);
        book.setBState(0);
        bookMapper.update(book);

        return true;
    }

    @Transactional
    @Override
    public List<Map<String,Object>> sendWarningMail(String type) {

        List<Map<String, Object>> list = recordMapper.revertWarning(type);

        System.out.println("RecordService---"+ list);

        if (!list.isEmpty()) {
            List<Message> messages = new ArrayList<>();
            for (Map<String, Object> map : list) {
                Message message = new Message();
                message.setTitle("关于" + map.get("b_name") + "图书归还提醒");
                message.setContent("尊敬的用户：你好，您借阅的《" + map.get("b_name") + "》图书将于 " + map.get("sub_num") + "天后(" + map.get("end_time") + ") 到期，请及时归还。");
                message.setSendTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(System.currentTimeMillis()));
                message.setUserId(Integer.parseInt(map.get("user_id").toString()));
                message.setRecordId(Integer.parseInt(map.get("record_id").toString()));
                message.setMessDay(Integer.parseInt(map.get("sub_num").toString()));
                messages.add(message);
            }
            messageMapper.add(messages);
        }
        return list;
    }


}
